package com.suda.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suda.dao.WarehouseMapper;
import com.suda.pojo.Warehouse;
import com.suda.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:Warehouse业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;


    /**
     * Warehouse条件+分页查询
     * @param warehouse 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Warehouse> findPage(Warehouse warehouse, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(warehouse);
        //执行搜索
        return new PageInfo<Warehouse>(warehouseMapper.selectByExample(example));
    }

    /**
     * Warehouse分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Warehouse> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Warehouse>(warehouseMapper.selectAll());
    }

    /**
     * Warehouse条件查询
     * @param warehouse
     * @return
     */
    @Override
    public List<Warehouse> findList(Warehouse warehouse){
        //构建查询条件
        Example example = createExample(warehouse);
        //根据构建的条件查询数据
        return warehouseMapper.selectByExample(example);
    }


    /**
     * Warehouse构建查询对象
     * @param warehouse
     * @return
     */
    public Example createExample(Warehouse warehouse){
        Example example=new Example(Warehouse.class);
        Example.Criteria criteria = example.createCriteria();
        if(warehouse!=null){
            // 仓库id
            if(!StringUtils.isEmpty(warehouse.getWarehouseId())){
                    criteria.andEqualTo("warehouseId",warehouse.getWarehouseId());
            }
            // 仓库名字
            if(!StringUtils.isEmpty(warehouse.getWarehouseName())){
                    criteria.andEqualTo("warehouseName",warehouse.getWarehouseName());
            }
            // 仓库联系电话
            if(!StringUtils.isEmpty(warehouse.getWarehousePhone())){
                    criteria.andEqualTo("warehousePhone",warehouse.getWarehousePhone());
            }
            // 仓库地址
            if(!StringUtils.isEmpty(warehouse.getWarehouseAddress())){
                    criteria.andEqualTo("warehouseAddress",warehouse.getWarehouseAddress());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        warehouseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Warehouse
     * @param warehouse
     */
    @Override
    public void update(Warehouse warehouse){
        warehouseMapper.updateByPrimaryKey(warehouse);
    }

    /**
     * 增加Warehouse
     * @param warehouse
     */
    @Override
    public void add(Warehouse warehouse){
        warehouseMapper.insert(warehouse);
    }

    /**
     * 根据ID查询Warehouse
     * @param id
     * @return
     */
    @Override
    public Warehouse findById(String id){
        return  warehouseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Warehouse全部数据
     * @return
     */
    @Override
    public List<Warehouse> findAll() {
        return warehouseMapper.selectAll();
    }
}
